package com.healthai.controller;

import com.healthai.dto.request.ChangePasswordRequest;
import com.healthai.dto.request.LoginRequest;
import com.healthai.dto.request.RegisterRequest;
import com.healthai.dto.request.UpdateUserRequest;
import com.healthai.dto.response.ApiResponse;
import com.healthai.dto.response.LoginResponse;
import com.healthai.dto.response.UserInfoResponse;
import com.healthai.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/auth")
public class AuthController {
    
    @Autowired
    private UserService userService;
    
    /**
     * 用户注册
     * @param registerRequest 注册请求
     * @return 注册结果
     */
    @PostMapping("/register")
    public ApiResponse<Integer> register(@Validated @RequestBody RegisterRequest registerRequest) {
        Integer userId = userService.register(registerRequest);
        return ApiResponse.success("注册成功", userId);
    }
    
    /**
     * 用户登录
     * @param loginRequest 登录请求
     * @return 登录结果
     */
    @PostMapping("/login")
    public ApiResponse<LoginResponse> login(@Validated @RequestBody LoginRequest loginRequest) {
        System.out.println("-----------------------------------------------------");
        LoginResponse loginResponse = userService.login(loginRequest);

        return ApiResponse.success("登录成功", loginResponse);
    }
    
    /**
     * 获取当前用户信息
     * @return 用户信息
     */
    @GetMapping("/userinfo")
    public ApiResponse<UserInfoResponse> getUserInfo() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String username = authentication.getName();
        UserInfoResponse userInfo = userService.getUserInfo(username);
        return ApiResponse.success("获取用户信息成功", userInfo);
    }
    
    /**
     * 更新当前用户信息
     * @param updateUserRequest 更新用户信息请求
     * @return 更新后的用户信息
     */
    @PutMapping("/userinfo")
    public ApiResponse<UserInfoResponse> updateUserInfo(@Validated @RequestBody UpdateUserRequest updateUserRequest) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String username = authentication.getName();
        UserInfoResponse userInfo = userService.updateUserInfo(username, updateUserRequest);
        return ApiResponse.success("更新用户信息成功", userInfo);
    }
    
    /**
     * 修改密码
     * @param changePasswordRequest 修改密码请求
     * @return 修改结果
     */
    @PostMapping("/change-password")
    public ApiResponse<Boolean> changePassword(@Validated @RequestBody ChangePasswordRequest changePasswordRequest) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String username = authentication.getName();
        boolean result = userService.changePassword(username, changePasswordRequest);
        return ApiResponse.success("修改密码成功", result);
    }
}